package com.jiazhong.boot.redis.shuxin.test;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.List;
import java.util.Set;

@SpringBootTest
@Slf4j
public class ZSetApp {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void a() {
        ZSetOperations<String, String> forZSet = stringRedisTemplate.opsForZSet();
        forZSet.add("A", "aa", 1.3);
        forZSet.add("A", "bb", 1.9);
        forZSet.add("A", "cc", 2.7);
        forZSet.add("A", "dd", 1.1);
        forZSet.add("A", "ee", 1.5);
        forZSet.addIfAbsent("A", "aa", 1.6);
        forZSet.addIfAbsent("A", "ff", 0.7);
        forZSet.add("B", "aa", 2.6);
        forZSet.add("B", "oo", 2.7);
        forZSet.add("B", "cc", 1.5);
    }

    @Test
    public void b() {
        ZSetOperations<String, String> forZSet = stringRedisTemplate.opsForZSet();
        Set<String> all = forZSet.range("A", 0, -1);
        log.info("all:{}", all);
        Set<String> set = forZSet.range("A", 0, 2);
        log.info("set:{}", set);
        String randomMember = forZSet.randomMember("A");
        List<String> randomMembers = forZSet.randomMembers("A", 3);
        log.info("randomMember:{}", randomMember);
        log.info("randomMembers:{}", randomMembers);
    }

    @Test
    public void c() {
        ZSetOperations<String, String> forZSet = stringRedisTemplate.opsForZSet();
        Set<String> union = forZSet.union("A", "B");
        log.info("union:{}", union);
        Set<String> difference = forZSet.difference("A", "B");
        log.info("difference:{}", difference);
        Set<String> intersect = forZSet.intersect("A", "B");
        log.info("intersect:{}", intersect);
        Long size = forZSet.size("A");
        log.info("size:{}", size);
        forZSet.remove("A", "aa");
        forZSet.popMax("A");
        forZSet.popMin("A");
    }
}
